
#include <cstdio>
#include <iostream>
#include <string_view>

#include <Windows.h>

using namespace std;

int main(int argc, char* argv[]) {
    if (argc < 2) {
        // no args provided
        fprintf(stdout, "Usage: %s <path>\n", argv[0]);
        return 1;
    }

    HANDLE hFile = CreateFileA(
        argv[1],
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL/*lpSecurityAttributes*/,
        OPEN_EXISTING,
        0/*dwFlagsAndAttributes*/,
        NULL/*hTemplateFile*/);
    if (INVALID_HANDLE_VALUE == hFile) {
        DWORD dwErr = GetLastError();
        fprintf(stderr, "Failed to CreateFileA, dwErr=%d\n", dwErr);
        return 1;
    }

    char buffer[1024] = {0};
    const int cbMaxRead = (sizeof(buffer) / sizeof(char)); // always 1024

    while (true) {
        DWORD cbRead;
        if (FALSE == ReadFile(hFile, &buffer[0], cbMaxRead, &cbRead, NULL/*lpOverlapped*/)) {
            DWORD dwErr = GetLastError();
            fprintf(stderr, "Failed to ReadFile, dwErr=%d\n", dwErr);
            return 1;
        }

        if (0 == cbRead) {
            // reach EOF
            break;
        }
        cout << string_view(buffer, cbRead);
    }
    CloseHandle(hFile);
    return 0;
}

// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
// https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle

// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile
// https://docs.microsoft.com/en-us/windows/win32/fileio/testing-for-the-end-of-a-file

// https://en.cppreference.com/w/cpp/header/cstdio
